$(function () {
    $("#jqGrid").Grid({
        url: '../stock/transfer/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '源门店', name: 'sourceBranchName', index: 'branch_id', width: 80},
			{label: '源仓库', name: 'sourceStorageName', index: 'branch_id', width: 80},
			{label: '目标门店', name: 'targetBranchName', index: 'branch_id', width: 80},
			{label: '目标仓库', name: 'targetStorageName', index: 'branch_id', width: 80},
			{label: '操作员', name: 'personName', index: 'person_id', width: 80},
			{label: '车辆VIN', name: 'vin', index: 'vin', width: 80},
			{label: '调拨日期', name: 'transferDate', index: 'transfer_date', width: 80},
			{label: '车辆类型', name: 'carType', index: 'carType', width: 80},
			{label: '品牌', name: 'carModel', index: 'carModel', width: 80},
			{label: '车型', name: 'carSerie', index: 'carSerie', width: 80},
			{label: '配置', name: 'config', index: 'config', width: 80},
			{label: '备注', name: 'content', index: 'content', width: 80}]
    });
    
//    var autoWidth = ($(window).width() - 450) / 13;
    $("#detailInnerTable").Grid({
        datatype: "local",
        data:vm.stockInner.productList,
        width:600,
//        cellEdit: true,  //开启表格编辑
        cellsubmit: 'clientArray', //remote代表每次编辑提交后进行服务器保存，clientArray只保存到数据表格不保存到服务器
        height: 250,
        shrinkToFit: false,
        autoScroll:true,//设置水平滚动条
//        left:90,
        rowNum:99999,
        page: false,
        rowList: false,
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '仓库', name: 'storageName', index: 'storage_id', width: 80},
			{label: '车辆类型', name: 'carType', index: 'car_type', width: 80},
			{label: '车系', name: 'carSerie', index: 'car_serie', width: 80},
			{label: '车型', name: 'carModel', index: 'car_model', width: 80},
			{label: '款式', name: 'design', index: 'design', width: 80},
			{label: '入库时间', name: 'innerDate', index: 'inner_date', width: 80},
			{label: '库龄天数', name: 'days', index: 'innerDate', width: 80,align:'center', formatter: function (value) {
					if(value<=45){
						return "<span style='display:block;background-color:#0066cc;margin:0;padding:0;color:#fff;'>"+value+"</span>";
					}else if(value <= 60){
						return "<span style='display:block;background-color:#00cc00;margin:0;padding:0;color:#fff;'>"+value+"</span>";
					}else if(value <= 90){
						return "<span style='display:block;background-color:#ffff00;margin:0;padding:0;color:#fff;'>"+value+"</span>";
					}else if(value <= 120){
						return "<span style='display:block;background-color:#c00;margin:0;padding:0;color:#fff;'>"+value+"</span>";
					}else{
						return value;
					}
				}
			},
			{label: '车辆vin', name: 'vin', index: 'vin', width: 80},
			{label: '发动机号', name: 'engine', index: 'engine', width: 80},
			{label: '指导价格', name: 'guidePrice', index: 'guide_price', width: 80},
			{label: '销售限价', name: 'advicePrice', index: 'advice_price', width: 80}
        ]
    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
        numRow:1,
        lastEditRow:0,
        lastEditCell:0,
		stockInner: {},
		ruleValidate: {
//			name: [
//				{required: true, message: '名称不能为空', trigger: 'blur'}
//			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		editBlur:function(rowid,val){
			for (var i = 0; i < vm.stockInner.productList.length; i++) {
				if(vm.stockInner.productList[i].id==rowid){
					vm.stockInner.productList[i][cellname]=value;
					break;
				}
			}
		},
		selectStorage:function(){
			openWindow({
            	type: 2,
                title: "选择仓库",
                area: ['80%', '60%'],
                content: ['../product/storageinfo.html?StockTransfer=1'],
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                	var selectRowData = window[layero.find('iframe')[0]['name']].getSelectedRowData('#jqGrid');
                	vm.stockInner.storageId=selectRowData.id;
                	vm.stockInner.storageName=selectRowData.name;
                    layer.close(index);
                }
            });
		},
		addCarModel:function(){
			openWindow({
            	type: 2,
                title: "选择车辆",
                area: ['80%', '60%'],
                content: ['../stock/stockreal.html?topClassifyId=car01&state=1&StockTransfer=1'],
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                	var selectRowData = window[layero.find('iframe')[0]['name']].getSelectedRowsData('#jqGrid');
                	if(null!=selectRowData){
                		if(selectRowData.length){
                			for (var i = 0; i < selectRowData.length; i++) {
                				var pushResult=true;
                    			for (var j = 0; j < vm.stockInner.productList.length; j++) {
    								if(vm.stockInner.productList[j].id==selectRowData[i].id){
    									pushResult=false;
    									break;
    								}
    							}
                    			if(pushResult){
                    				selectRowData[i].state=1;
                    				vm.stockInner.productList.push(selectRowData[i]);
                    			}
							}
                		}else{
                			var pushResult=true;
                			for (var i = 0; i < vm.stockInner.productList.length; i++) {
								if(vm.stockInner.productList[i].id==selectRowData.id){
									pushResult=false;
									break;
								}
							}
                			if(pushResult){
                				selectRowData.state=1;
                				vm.stockInner.productList.push(selectRowData);
                			}
                		}
                	}
                    layer.close(index);
                    vm.reloadDetail();
                }
            });
		},
		routeCarModel:function(){
			openWindow({
				type: 2,
				title: "选择在途车辆",
				area: ['80%', '60%'],
				content: ['../stock/stockroute.html'],
				btn: ['确定', '取消'],
				btn1: function (index,layero) {
					var selectRowData = window[layero.find('iframe')[0]['name']].getSelectedRowData('#jqGrid');
					selectRowData.topClassifyId="rot01";
					selectRowData.topClassifyName="在途车辆";
					selectRowData.allName=selectRowData.productName;
					selectRowData.id=vm.numRow;
					vm.numRow+=1;
					vm.stockInner.productList.push(selectRowData);
					layer.close(index);
					vm.reloadDetail();
				}
			});
		},
		copyCarModel:function(){
			var id = getSelectedRow("#detailInnerTable");
			if (id == null) {
				return;
			}
			var otherData={};
			for (var i = 0; i < vm.stockInner.productList.length; i++) {
				if(vm.stockInner.productList[i].id==id){
					otherData=Object.assign({},vm.stockInner.productList[i]);
					break;
				}
			}
			otherData.id=vm.numRow;
			vm.numRow+=1;
			vm.stockInner.productList.push(otherData);
			vm.reloadDetail();
		},
		delRow:function(){
			var id = getSelectedRow("#detailInnerTable");
			if (id == null) {
				return;
			}
			var otherDataList=[];
			for (var i = 0; i < vm.stockInner.productList.length; i++) {
				if(vm.stockInner.productList[i].id==id){
					continue;
				}
				otherDataList.push(vm.stockInner.productList[i]);
			}
			vm.stockInner.productList=otherDataList;
			vm.reloadDetail();
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.stockInner = {productList:[],storageId:null,storageName:""};
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.stockInner.id == null ? "../stock/transfer/batch" : "../stock/inner/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.stockInner),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../stock/inner/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../stockinner/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.stockInner = r.stockInner;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
		reloadDetail:function(){
			$("#detailInnerTable").jqGrid('clearGridData');  //清空表格
            $("#detailInnerTable").jqGrid('setGridParam', {  // 重新加载数据
                datatype: 'local',
                data: vm.stockInner.productList,   //  data 是符合格式要求的需要重新加载的数据
                page: 1
            }).trigger("reloadGrid");
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});